package za.co.smartmi.service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import za.co.smartmi.service.exception.ServiceExceptionFactory;
import za.co.smartmi.util.AppConstants;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import za.co.smartmi.dao.SystemUserDao;
import za.co.smartmi.dao.SystemUserRoleDao;
import za.co.smartmi.dao.exceptions.SystemUserDaoException;
import za.co.smartmi.dao.exceptions.SystemUserRoleDaoException;
import za.co.smartmi.model.SystemAccount;
import za.co.smartmi.model.SystemUser;
import za.co.smartmi.model.SystemUserRole;

import za.co.smartmi.service.exception.ServiceException;

@Service
public class SystemAccountServiceImpl implements SystemAccountService {

    private static final Logger log = Logger.getLogger(SystemAccountServiceImpl.class);
    @Autowired
    private SystemUserDao systemUserDao;
    @Autowired
    private SystemUserRoleDao systemUserRoleDao;

    public List<SystemUserRole> getUserRoles(String username) throws ServiceException {
        List<SystemUserRole> userRoles;
        try {
            userRoles = systemUserRoleDao.findWhereUsernameEquals(username);
        } catch (SystemUserRoleDaoException ex) {
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_GET_SYSTEM_ACCOUNT_ROLES, ex);
        }
        if (userRoles == null) {
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_GET_SYSTEM_ACCOUNT_ROLES_NOT_EXIST, null);
        }
        return userRoles;
    }

    @Transactional
    public SystemAccount getSystemAccount(String username) throws ServiceException {

        SystemAccount systemAccount = new SystemAccount();
        SystemUser systemUser;
        try {
            systemUser = this.systemUserDao.findByPrimaryKey(username);

        } catch (SystemUserDaoException e) {
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_GET_SYSTEM_ACCOUNT, e);
        }
        if (systemUser == null) {
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_GET_SYSTEM_ACCOUNT_NOT_EXIST, null);
        }

        //List<SystemUserRole> userRoles = getUserRoles(username);
        //systemAccount.getSystemUserRole().addAll(userRoles);
        systemAccount.setSystemUser(systemUser);
        return systemAccount;
    }

    private void createZipFile(String folder, String destFile) throws IOException {
        FileOutputStream dest = new FileOutputStream(new File(destFile));
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
        out.setMethod(ZipOutputStream.DEFLATED);
        out.setLevel(9);
        File subDir = new File(folder);
        try {
            String subdirList[] = subDir.list();
            for (String sd : subdirList) {
                File f = new File(folder + "/" + sd);
                FileInputStream fi = null;
                try {
                    fi = new FileInputStream(f);
                    ZipEntry entry = new ZipEntry(sd);
                    out.putNextEntry(entry);
                    IOUtils.copy(fi, out);
                } finally {
                    if (fi != null) {
                        fi.close();
                        f.delete();
                    }
                }
                out.flush();
            }
        } finally {
            if (out != null) {
                out.close();
            }
            if (dest != null) {
                dest.close();
            }
        }
    }
}
